package com.ruoyi.ftrl.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ftrl.domain.RoomInfo;
import com.ruoyi.ftrl.mapper.RoomInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ftrl.mapper.TemperatureControlMapper;
import com.ruoyi.ftrl.domain.TemperatureControl;
import com.ruoyi.ftrl.service.ITemperatureControlService;


/**
 * 温控阀管理Service业务层处理
 * 
 * @author dihang
 * @date 2024-08-08
 */
@Service
public class TemperatureControlServiceImpl implements ITemperatureControlService 
{
    @Autowired
    private TemperatureControlMapper temperatureControlMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    /**
     * 查询温控阀管理
     * 
     * @param id 温控阀管理主键
     * @return 温控阀管理
     */
    @Override
    public TemperatureControl selectTemperatureControlById(Long id)
    {
        return temperatureControlMapper.selectTemperatureControlById(id);
    }

    /**
     * 查询温控阀管理列表
     * 
     * @param temperatureControl 温控阀管理
     * @return 温控阀管理
     */
    @Override
    public List<TemperatureControl> selectTemperatureControlList(TemperatureControl temperatureControl)
    {
        return temperatureControlMapper.selectTemperatureControlList(temperatureControl);
    }

    /**
     * 新增温控阀管理
     * 
     * @param temperatureControl 温控阀管理
     * @return 结果
     */
    @Override
    public int insertTemperatureControl(TemperatureControl temperatureControl)
    {
        temperatureControl.setCreateTime(DateUtils.getNowDate());
        temperatureControl.setCreateBy(SecurityUtils.getUsername());
        //新增设备时将设备绑定的房间编号带入
        RoomInfo roomInfo=new RoomInfo();
        roomInfo.setCommunityName(temperatureControl.getCommunityName());
        roomInfo.setBuildingName(temperatureControl.getBuildingName());
        roomInfo.setUnitName(temperatureControl.getUnitName());
        roomInfo.setHouseNumber(temperatureControl.getRoomAme());
        List<RoomInfo> roomInfos = roomInfoMapper.selectRoomInfoList(roomInfo);

        if (roomInfos.size() > 1){//添加单元阀
            //生成设备绑定单元的唯一编码
            String roomNo = generateUniqueRoomNumber();
            temperatureControl.setRoomNo(roomNo);

        }else if (roomInfos.size()==1){//添加户阀
            RoomInfo roomInfo1 = roomInfos.get(0);
            temperatureControl.setRoomNo(roomInfo1.getRoomNo());
            //户主姓名
            temperatureControl.setUserName(roomInfo1.getPersonName());
        }else {//如果设备绑定房间不存在，抛出异常
            throw new ServiceException("您所绑定该设备对应的房间不存在，请先添加后再进行绑定！");
        }
        return temperatureControlMapper.insertTemperatureControl(temperatureControl);
    }

    /**
     *  生成唯一房间编号
     * @return
     */
    private static Set<String> generatedNumbers = new HashSet<>();

    public static String generateUniqueRoomNumber() {
        String prefix = "42260";
        Random random = new Random();
        String roomNumber;
        do {
            int suffix = random.nextInt(100000);
            String formattedSuffix = String.format("%05d", suffix);
            roomNumber = prefix + formattedSuffix;
        } while (generatedNumbers.contains(roomNumber));
        generatedNumbers.add(roomNumber);
        return roomNumber;
    }

    /**
     * 修改温控阀管理
     * 
     * @param temperatureControl 温控阀管理
     * @return 结果
     */
    @Override
    public int updateTemperatureControl(TemperatureControl temperatureControl)
    {
        temperatureControl.setUpdateTime(DateUtils.getNowDate());
        return temperatureControlMapper.updateTemperatureControl(temperatureControl);
    }

    /**
     * 批量删除温控阀管理
     * 
     * @param ids 需要删除的温控阀管理主键
     * @return 结果
     */
    @Override
    public int deleteTemperatureControlByIds(Long[] ids)
    {
        return temperatureControlMapper.deleteTemperatureControlByIds(ids);
    }

    /**
     * 删除温控阀管理信息
     * 
     * @param id 温控阀管理主键
     * @return 结果
     */
    @Override
    public int deleteTemperatureControlById(Long id)
    {
        return temperatureControlMapper.deleteTemperatureControlById(id);
    }

    /**
     * 查询采集器下的温控阀管理列表
     */
    @Override
    public List<TemperatureControl> selectTemperatureControlLists(Long id) {
        List<TemperatureControl> list = temperatureControlMapper.selectTemperatureControlLists(id);
        for (TemperatureControl temperatureControl : list) {
            //安装位置
            temperatureControl.setInstall(temperatureControl.getCommunityName()+"-"+temperatureControl.getBuildingName()+"-"+temperatureControl.getUnitName()+"-"+temperatureControl.getRoomAme());

        }
        return list;
    }
}
